package com.heepay.common;

import com.heepay.api.DefaultHeepayClient;
import com.heepay.api.HeepayClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;


public class Utils {

    /**
     * @param bean
     * @return
     * @throws IntrospectionException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    public static Map convertBeanNoneEmpty(Object bean)
            throws IntrospectionException, IllegalAccessException, InvocationTargetException {
        Class type = bean.getClass();
        Map returnMap = new HashMap();
        BeanInfo beanInfo = Introspector.getBeanInfo(type);

        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (int i = 0; i < propertyDescriptors.length; i++) {
            PropertyDescriptor descriptor = propertyDescriptors[i];
            String propertyName = descriptor.getName();
            if (!propertyName.equals("class") && !propertyName.equals("sign")) {
                Method readMethod = descriptor.getReadMethod();
                Object result = readMethod.invoke(bean, new Object[0]);
                if (result != null) {
                    returnMap.put(propertyName, result);
                }
            }
        }
        return returnMap;
    }

    /**
     * @param data
     * @return
     */
    public static String coverMap2String(Map<String, String> data) {
        TreeMap<String, String> tree = new TreeMap<>();
        Iterator<Map.Entry<String, String>> it = data.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, String> en = it.next();
            tree.put(en.getKey(), en.getValue());
        }
        it = tree.entrySet().iterator();
        StringBuilder sb = new StringBuilder();
        while (it.hasNext()) {
            Map.Entry<String, String> en = it.next();
            sb.append(en.getKey()).append("=").append(en.getValue()).append("&");
        }
        return sb.substring(0, sb.length() - 1);
    }

    public static byte[] readResourceFileByBytes(String fileName) throws IOException {
        InputStream in = null;
        ByteArrayOutputStream out = null;
        try {
            ClassPathResource classPathResource = new ClassPathResource(fileName);
            in = classPathResource.getInputStream();
            out = new ByteArrayOutputStream();
            byte[] tempbytes = new byte[in.available()];
            for (int i = 0; (i = in.read(tempbytes)) != -1; ) {
                out.write(tempbytes, 0, i);
            }
        } finally {
            in.close();
            out.close();
        }
        return out.toByteArray();
    }

    public static byte[] readFileByBytes(String fileName) throws IOException {
        InputStream in = null;
        ByteArrayOutputStream out = null;
        try {
            in = new BufferedInputStream(new FileInputStream(fileName));
            out = new ByteArrayOutputStream();
            byte[] tempbytes = new byte[in.available()];
            for (int i = 0; (i = in.read(tempbytes)) != -1; ) {
                out.write(tempbytes, 0, i);
            }
        } finally {
            in.close();
            out.close();
        }
        return out.toByteArray();
    }

}
